<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/02/09 13:32:14 $)
// $RCSfile: staff_views.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("staff_ticketviews");

require_once ("./modules/tickets/functions_ticketsui.php");
require_once ("./modules/tickets/functions_views.php");


/**
* ###############################################
* NEW TICKET VIEW
* ###############################################
*/
if ($eventaction == "newticketview")
{
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || empty($_POST["ticketsperpage"]))
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!_is_array($_POST["viewtickettypes"])) {
			$errormessage = $_SWIFT["language"]["errorviewtype"];
		} else if (!count($_POST["fields"])) {
			$errormessage = $_SWIFT["language"]["errornofields"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			// All is well...
			// Process fields
			$_fields = processFieldsFromPost($_POST["fields"]);
			createTicketView($_POST["title"], iif($_POST["viewtype"]==VIEW_PUBLIC, VIEW_PUBLIC, VIEW_PRIVATE), iif(in_array(VIEW_ALLTICKETS, $_POST["viewtickettypes"]), "1", "0"), iif(in_array(VIEW_UNASSIGNED, $_POST["viewtickettypes"]), "1", "0"), iif(in_array(VIEW_ASSIGNED, $_POST["viewtickettypes"]), "1", "0"), intval($_POST["sortby"]), intval($_POST["sortorder"]), intval($_POST["ticketsperpage"]), $_fields, false);

			printRedirect(sprintf($_SWIFT["language"]["viewinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_POST["departmentid"]) ."&ticketstatusid=". intval($_POST["ticketstatusid"]) ."&ticketsearchid=". intval($_POST["ticketsearchid"]) ."&ticketlabelid=". intval($_POST["ticketlabelid"]) ."&newview=".urlencode($_POST["title"]));
			exit;
		}
	}

	renderTicketTree();

	$interface->staffHeader($_SWIFT["language"]["ticket"] ." &gt; ".$_SWIFT["language"]["insertview"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=newticketview" title="'.$_SWIFT["language"]["insertview"].'">'.$_SWIFT["language"]["insertview"]."</a>", "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderViewForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manage");
	$interface->staffFooter();






/**
* ###############################################
* MANAGE TICKET VIEWS
* ###############################################
*/
} else if ($eventaction == "editticketview") {
	// First get the view
	$_ticketview = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketviews` WHERE `ticketviewid` = '". intval($_REQUEST["ticketviewid"]) ."';");
	if (empty($_ticketview["ticketviewid"]) || ($_ticketview["viewtype"] == VIEW_PRIVATE && $_ticketview["staffid"] != $_SWIFT["staff"]["staffid"]))
	{
		echo $_SWIFT["language"]["viewediterror"];
		exit;
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || empty($_POST["ticketsperpage"]))
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!_is_array($_POST["viewtickettypes"])) {
			$errormessage = $_SWIFT["language"]["errorviewtype"];
		} else if (!count($_POST["fields"])) {
			$errormessage = $_SWIFT["language"]["errornofields"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$_fields = processFieldsFromPost($_POST["fields"]);
			updateTicketView($_ticketview["ticketviewid"], $_POST["title"], iif($_POST["viewtype"]==VIEW_PUBLIC, VIEW_PUBLIC, VIEW_PRIVATE), iif(in_array(VIEW_ALLTICKETS, $_POST["viewtickettypes"]), "1", "0"), iif(in_array(VIEW_UNASSIGNED, $_POST["viewtickettypes"]), "1", "0"), iif(in_array(VIEW_ASSIGNED, $_POST["viewtickettypes"]), "1", "0"), intval($_POST["sortby"]), intval($_POST["sortorder"]), intval($_POST["ticketsperpage"]), $_fields);

			printRedirect(sprintf($_SWIFT["language"]["vieweditconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_POST["departmentid"]) ."&ticketstatusid=". intval($_POST["ticketstatusid"]) ."&ticketsearchid=". intval($_POST["ticketsearchid"]) ."&ticketlabelid=". intval($_POST["ticketlabelid"]) ."&editview=".urlencode($_POST["title"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_ticketview);

	renderTicketTree();
	$interface->staffHeader($_SWIFT["language"]["ticket"] ." &gt; ".$_SWIFT["language"]["editview"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editticketview&ticketviewid='. intval($_ticketview["ticketviewid"]) .'" title="'.$_SWIFT["language"]["editview"].'">'.$_SWIFT["language"]["editview"]."</a>", "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderViewForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manage");
	$interface->staffFooter();









/**
* ###############################################
* DELETE TICKET VIEW
* ###############################################
*/
} else if ($eventaction == "delticketview") {
	// First get the view
	$_ticketview = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketviews` WHERE `ticketviewid` = '". intval($_REQUEST["ticketviewid"]) ."';");
	if (empty($_ticketview["ticketviewid"]) || ($_ticketview["viewtype"] == VIEW_PRIVATE && $_ticketview["staffid"] != $_SWIFT["staff"]["staffid"]) || $_SWIFT["isdemo"] == true)
	{
		echo $_SWIFT["language"]["viewediterror"];
		exit;
	}

	if($_SWIFT["staff"]["tdeleteview"] != '0')
	{
		deleteTicketViews(array($_REQUEST["ticketviewid"]));
	}else {
		echo $_SWIFT["language"]["viewdeleteerror"];
		exit;
	}

	printRedirect(sprintf($_SWIFT["language"]["viewdelconfirm"], htmlspecialchars($_ticketview["title"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_POST["departmentid"]) ."&ticketstatusid=". intval($_POST["ticketstatusid"]) ."&ticketsearchid=". intval($_POST["ticketsearchid"]) ."&ticketlabelid=". intval($_POST["ticketlabelid"]) ."&delview=".urlencode($_ticketview["title"]));
	exit;
}

?>